package org.waveplayer.section;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.waveplayer.section.decoration.SectionItemDecoration;
import org.waveplayer.section.decoration.TextSectionPainter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private boolean initialized;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recyclerView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!initialized) {
            initialized = true;

            List<Data> list = new ArrayList<>();
            String date = "2021-04-";
            int startDate = 10;
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < (int) (3 + Math.random() * 12); j++) {
                    Data data = new Data();
                    data.date = date + startDate;
                    list.add(data);
                }
                startDate++;
            }

            recyclerView.setLayoutManager(new LinearLayoutManager(this));

            DataSelectionProvider sectionProvider = new DataSelectionProvider(list);
            TextSectionPainter sectionPainter = new TextSectionPainter.Builder(this)
                    .setSectionBarColor(0xffffacac)
                    .setDividerColor(0xffff8888)
                    .setTextColor(0xffffffff)
                    .setTextSize(dip2px(16))
                    .setSectionBarHeight(dip2px(32))
                    .setDividerHeight(1)
                    .build();
            SectionItemDecoration decoration = new SectionItemDecoration(sectionProvider, sectionPainter);

            recyclerView.addItemDecoration(decoration);
            recyclerView.setAdapter(new ArrayAdapter(list.toArray(new Object[]{})));


        }
    }

    int dip2px(int dp) {
        return (int) (getResources().getDisplayMetrics().density * dp + .5);
    }
}